﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms;
using System.IO;

namespace MacomberMapSystem.Common.User_Interfaces.Editors
{
    /// <summary>
    /// This class offers a folder browser
    /// </summary>
    public class MM_Folder_Browser: UITypeEditor
    {
        /// <summary>
        /// Allow editing of our folder, converting paths from local to UNC paths if not on this machine.
        /// </summary>
        /// <param name="context"></param>
        /// <param name="provider"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            String CurValue = Convert.ToString(value);

            using (SaveFileDialog sFd = new SaveFileDialog())
            {
                sFd.AutoUpgradeEnabled = true;
                sFd.CreatePrompt = false;
                sFd.CheckFileExists = false;
                sFd.CheckPathExists = true;
                sFd.DereferenceLinks = true;
                sFd.OverwritePrompt = false;
                sFd.SupportMultiDottedExtensions = true;
                sFd.Title = context.PropertyDescriptor.Name + ": " + context.PropertyDescriptor.Description;
                sFd.ValidateNames = false;                
                sFd.FileName = "Please select a folder...";
                sFd.Filter = "Please select a folder|Please select a folder";
                if (sFd.ShowDialog() == DialogResult.OK)
                    return Path.GetDirectoryName(sFd.FileName);
                else
                    return CurValue;
            }
        }
        
        /// <summary>
        /// Report the editing style for our control
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }
}
